package com.oauth.google.client.ui.orkut.tabs;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;
import com.oauth.google.client.beans.orkut.OrkutBirthday;
import com.oauth.google.client.beans.orkut.OrkutBirthdayDetails;
import com.oauth.google.client.rpc.orkut.OrkutRemoteServiceAsync;
import com.oauth.google.client.ui.GoogleLoadingPanel;
import com.oauth.google.client.ui.orkut.ConnectionPanel;

public class BirthdaysTab extends Composite {

	private static FriendsTabUiBinder uiBinder = GWT
			.create(FriendsTabUiBinder.class);

	interface FriendsTabUiBinder extends UiBinder<FlowPanel, BirthdaysTab> {
	}

	@UiField
	VerticalPanel communitiesPanel;

	@UiField
	GoogleLoadingPanel loadingPanel;

	private OrkutRemoteServiceAsync service;

	public BirthdaysTab(OrkutRemoteServiceAsync pService) {
		service = pService;
		initWidget(uiBinder.createAndBindUi(this));
	}

	public void init() {
		loadingPanel.setVisible(true);
		communitiesPanel.clear();

		service
				.getOrkutBirthdays(new OAuthAsnyCallBack<OrkutBirthdayDetails>() {
					@Override
					public void onSuccess(OrkutBirthdayDetails pResult) {
						if (pResult != null && pResult.getBirthdays().isEmpty()) {
							communitiesPanel
									.add(new Label(
											"There is no upcoming birthdays.So you won't get any birthday treat for next few months. :("));
						} else {
							for (OrkutBirthday user : pResult.getBirthdays()) {
								communitiesPanel.add(new ConnectionPanel(user));
							}
						}
						loadingPanel.setVisible(false);
					}
				});
	}
}
